package cn.bestick.cblog.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.bestick.cblog.dmo.FriendLink;
import cn.bestick.cblog.dto.ArticleDto;
import cn.bestick.cblog.dto.MsgDto;
import cn.bestick.cblog.service.ArticleService;
import cn.bestick.cblog.service.FriendLinkService;
import cn.bestick.cblog.service.MsgService;
import cn.bestick.cblog.service.impl.ArticleServiceImpl;
import cn.bestick.cblog.service.impl.FriendLinkServiceImpl;
import cn.bestick.cblog.service.impl.MsgServiceImpl;
import cn.bestick.cblog.util.PageBean;
import cn.bestick.cblog.util.PropertiesUtil;
import cn.bestick.cblog.util.StringUtil;

public class HomeServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private ArticleService articleService = new ArticleServiceImpl();
	private MsgService msgServive = new MsgServiceImpl();
	private FriendLinkService friendLinkService = new FriendLinkServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.service(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.service(req, resp);
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");

		List<ArticleDto> recentArticles = articleService
				.getArticles(new PageBean(1, 10));
		getServletContext().setAttribute("recentArticles", recentArticles);

		List<MsgDto> recentMsgs = msgServive.getRecentMsgs();
		getServletContext().setAttribute("recentMsgs", recentMsgs);

		List<FriendLink> friendLinks = friendLinkService.getFriendLinks();
		getServletContext().setAttribute("friends", friendLinks);

		String page = request.getParameter("page");

		if (StringUtil.isEmpty(page)) {
			page = "1";
		}

		PageBean pageBean = new PageBean(Integer.parseInt(page),
				Integer.parseInt(PropertiesUtil.getValue("pageSize")));
		List<ArticleDto> articles = articleService.getArticles(pageBean);
		int total = articleService.count();
		String pageCode = this.genPagation(total, Integer.parseInt(page),
				Integer.parseInt(PropertiesUtil.getValue("pageSize")));
		request.setAttribute("pageCode", pageCode);

		request.setAttribute("articles", articles);
		request.setAttribute("mainPage", "template/article/list.jsp");
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

	private String genPagation(int totalNum, int currentPage, int pageSize) {
		int totalPage = totalNum % pageSize == 0 ? totalNum / pageSize
				: totalNum / pageSize + 1;
		StringBuffer pageCode = new StringBuffer();
		pageCode.append("<li><a href='home?page=1'>首页</a></li>");
		if (currentPage == 1) {
			pageCode.append("<li class='disabled'><a href='#'>上一页</a></li>");
		} else {
			pageCode.append("<li><a href='home?page=" + (currentPage - 1)
					+ "'>上一页</a></li>");
		}
		for (int i = currentPage - 2; i <= currentPage + 2; i++) {
			if (i < 1 || i > totalPage) {
				continue;
			}
			if (i == currentPage) {
				pageCode.append("<li class='active'><a href='#'>" + i
						+ "</a></li>");
			} else {
				pageCode.append("<li><a href='home?page=" + i + "'>" + i
						+ "</a></li>");
			}
		}
		if (currentPage == totalPage) {
			pageCode.append("<li class='disabled'><a href='#'>下一页</a></li>");
		} else {
			pageCode.append("<li><a href='home?page=" + (currentPage + 1)
					+ "'>下一页</a></li>");
		}
		pageCode.append("<li><a href='home?page=" + totalPage + "'>末页</a></li>");
		return pageCode.toString();
	}
}
